package com.gewu.pm.ui.fragment.salesperson;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.app.AppFragment;
import com.gewu.pm.ui.activity.customer.DeliveryProviderDetailsActivity;
import com.gewu.pm.ui.activity.customer.SalesPersonDetailsActivity;
import com.gewu.pm.ui.adapter.customer.SalespersonDeliveryAdapter;
import com.hjq.base.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * 业务员详情 - 配送商Tab
 * 
 * 功能：
 * 1. 搜索配送商（输入配送商名称/车辆编码）
 * 2. 显示配送商列表
 * 3. 支持点击配送商Item
 */
public final class SalespersonDeliveryFragment extends AppFragment<SalesPersonDetailsActivity> 
        implements BaseAdapter.OnItemClickListener {

    private TextView mRecordCountView;
    private EditText mSearchEdit;
    private RecyclerView mRecyclerView;
    
    private SalespersonDeliveryAdapter mAdapter;
    private List<String> mDeliveryList;

    /**
     * 创建Fragment实例
     */
    public static SalespersonDeliveryFragment newInstance() {
        return new SalespersonDeliveryFragment();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.salesperson_delivery_fragment;
    }

    @Override
    protected void initView() {
        mRecordCountView = findViewById(R.id.tv_record_count);
        mSearchEdit = findViewById(R.id.et_search);
        mRecyclerView = findViewById(R.id.rv_delivery_list);
        
        // 设置搜索框提示文字
        mSearchEdit.setHint(R.string.search_delivery_provider_hint);
        
        // 设置RecyclerView
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mAdapter = new SalespersonDeliveryAdapter(getContext());
        mAdapter.setOnItemClickListener(this);
        mRecyclerView.setAdapter(mAdapter);
        
        // 设置搜索监听
        mSearchEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO: 实现搜索请求接口（暂时不实现本地过滤）
                String keyword = s.toString().trim();
                if (keyword.isEmpty()) {
                    // 重新加载全部数据
                    loadData();
                } else {
                    // 发起搜索请求
                    // searchDeliveryProviders(keyword);
                }
            }
        });
    }

    @Override
    protected void initData() {
        // 初始化测试数据（4条配送商名称）
        mDeliveryList = new ArrayList<>();
        mDeliveryList.add("杭州小配送1号车");
        mDeliveryList.add("杭州小配送1号车");
        mDeliveryList.add("杭州小配送1号车");
        mDeliveryList.add("杭州小配送1号车");
        
        loadData();
    }

    /**
     * 加载数据
     */
    private void loadData() {
        mAdapter.setData(mDeliveryList);
        updateRecordCount();
    }

    /**
     * 更新记录数
     */
    private void updateRecordCount() {
        int count = mAdapter.getCount();
        mRecordCountView.setText(getString(R.string.record_count_format, count));
    }

    @Override
    public void onItemClick(RecyclerView recyclerView, View itemView, int position) {
        String deliveryName = mAdapter.getItem(position);
        // 跳转到配送商详情页面
        DeliveryProviderDetailsActivity.start(getContext(), deliveryName);
    }
}

